#!/bin/bash
source $SCRIPT_DIR/build_proxy_cfg.sh
source $SCRIPT_DIR/shlog.sh

sigterm_handler() {
  echo "Shutdown signal received."
  exit 1
}

## Setup signal trap
trap 'trap " " SIGINT SIGTERM SIGHUP; kill 0; wait; sigterm_handler' SIGINT SIGTERM SIGHUP

handleRtnVal() {
   if [ "$1" == "143" -o "$1" == "1" ]; then
     exit 1
   fi
}

if [ "$BuildAnd" == "true" ]; then
  # build Android
  echo "############1# begin build android #############"
  cd $SCRIPT_DIR
  echo "############2# begin build android #############"
  pwd
  #
  echo "############# 删除旧的buildInfo.json文件 #############"
  cd ../
  rm -rf "buildInfo.json"
  pwd
  if [ "$BuildType" == "test" ]; then
    echo "======$SCRIPT_DIR===="
    python3 jenkins/helper4VersionInfoSync4And.py
    echo "############# helper4VersionInfoSync4And #############"
    echo "############# helper4VersionInfoSync4And #############"
    export AND_APK_DIR="${JENKINS_HOME}/bak/${BuildType}/${BUILD_TAG}/and/"
    rm -rf $AND_APK_DIR
    mkdir -p ${AND_APK_DIR}
    buildLog "AND_APK_DIR = $AND_APK_DIR"
    export AND_APK_FILE_NAME="app-ci-release.apk"
    # 删除旧的文件
    echo "#############删除旧的文件1 ${AND_APK_FILE_NAME} #############"
    pwd
    ls
    rm -f app/build/outputs/apk/
    fastlane ci
    retVal=$?
    if [ $retVal -ne 0 ]; then
      buildLog "fastlane ci build failed！($retVal)"
      handleRtnVal "$retVal"
    else
      cp app/build/outputs/apk/ci/release/${AND_APK_FILE_NAME} ${AND_APK_DIR}
      export AND_APK_FILE_PATH=${AND_APK_DIR}${AND_APK_FILE_NAME}
    fi
 elif [ "$BuildType" == "pub" ]; then
    export GOOGLE_PLAY_INTERNAL_TRACK_VERSION_CODES="GOOGLE_PLAY_INTERNAL_TRACK_VERSION_CODES.tmp"
    buildLog "GOOGLE_PLAY_INTERNAL_TRACK_VERSION_CODES --------------------------------------------------------"
    buildLog "GOOGLE_PLAY_INTERNAL_TRACK_VERSION_CODES --------------------------------------------------------"
    rm -rf $GOOGLE_PLAY_INTERNAL_TRACK_VERSION_CODES
    if [ "$Upload2GooglePlay" == "true" ]; then
      # 获取GooglePlay最新版本号
      proxy_on
      fastlane run google_play_track_version_codes package_name:"com.starblink.android.guang" track:"internal" > $GOOGLE_PLAY_INTERNAL_TRACK_VERSION_CODES
      buildLog $(cat $GOOGLE_PLAY_INTERNAL_TRACK_VERSION_CODES)
      proxy_off
    fi
    # android should sync the version info from pubspec.yaml to guang_build.properties
    pwd
    buildLog "执行 helper4VersionInfoSync4And.py --------------------------------------------------------"
    buildLog "执行 helper4VersionInfoSync4And.py --------------------------------------------------------"
    python3 jenkins/helper4VersionInfoSync4And.py
    echo "执行 helper4VersionInfoSync4And.py -------------------------------------------------------- 执行完成 --------"
    echo "执行 helper4VersionInfoSync4And.py -------------------------------------------------------- 执行完成 --------"
    echo "执行 helper4VersionInfoSync4And.py --------------------------------------------------------AND_APK_DIR ${JENKINS_HOME}/bak/${BuildType}/${BUILD_TAG}/and/ "
    export AND_APK_DIR="${JENKINS_HOME}/bak/${BuildType}/${BUILD_TAG}/and/"
    rm -rf $AND_APK_DIR
    mkdir -p ${AND_APK_DIR}
    buildLog "-------------------------------------------------------- AND_APK_DIR = $AND_APK_DIR --------------------------------------------------------"
    export AND_APK_FILE_NAME="app-play-release.apk"
    export AND_AAB_FILE_NAME="app-play-release.aab"
    # 删除旧的文件
    rm -f app/build/outputs/apk/
    echo "-------------------------------------------------------- 执行 fastlane play 打包 -------------------------------------------------------- --------"
    echo "-------------------------------------------------------- 执行 fastlane play 打包 -------------------------------------------------------- --------"
    fastlane play
    retVal=$?
    if [ $retVal -ne 0 ]; then

      buildLog " ----------------------------标志位 1 ----------------------------"
      buildLog "fastlane play build failed！($retVal) --------------------------------------------------------"
      buildLog "fastlane play build failed！($retVal) --------------------------------------------------------"
      buildLog "fastlane play build failed！($retVal) --------------------------------------------------------"
      handleRtnVal "$retVal"
    else
      buildLog " --------------------"app/build/outputs/bundle/playRelease/"--------标志位 2 ----------------------------"
      cp app/build/outputs/apk/play/release/${AND_APK_FILE_NAME} ${AND_APK_DIR}
      pwd
      cp app/build/outputs/bundle/playRelease/${AND_AAB_FILE_NAME} ${AND_APK_DIR}
      pwd
      export AND_APK_FILE_PATH=${AND_APK_DIR}${AND_APK_FILE_NAME}
      export AND_AAB_FILE_PATH=${AND_APK_DIR}${AND_AAB_FILE_NAME}
    fi
  fi

  buildLog "############# end build android #############"
fi


echo "开始执行--------->artifact_upload-------------"
echo "开始执行--------->artifact_upload-------------"
echo "开始执行--------->artifact_upload-------------"
echo "开始执行--------->artifact_upload-------------"

sh $SCRIPT_DIR/artifact_upload.sh